<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/layui/css/layui.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/css/thesame.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/css/rightCon.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/css/popCss.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/css/page.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/css/tree.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/hiprint/css/hiprint.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/hiprint/css/print-lock.css'" media="all"/>
	<link rel="stylesheet" type="text/css" th:href="${#request.getContextPath()}+'/static/foodDelivery/hiprint/css/print-lock.css'" media="all"/>
	<style type="text/css">
		.current {
			background-color: rgba(28, 147, 153, 0.1);
			color: #1C9399;
		}
		.food-con ul{ display: block }
		.food-con ul li{ flex: auto; position: relative }
		.food-con ul li p:nth-child(1){ width: 95px; }
		.food-con ul li p:nth-child(2){ position: absolute; right:0; top: 0; text-align: right; }
	</style>
</head>

<body class="layui-layout-body">
<div class="layui-body pad12px">
	<!-- 内容主体区域 -->
	<div class="layui-box-con relative">
		<div class="layui-left-con layui-left-label-con pad12px">
			<div class="border-radius4 bgWhite box-shadows">
				<div class="tree-con-box" id="organizationDiv">

				</div>
			</div>
		</div>
		<div class="layui-right-con pad12px">
			<div class="border-radius4 bgWhite box-shadows">
				<div class="layui-right-box pad25px border-box">
					<nav class="layui-form more-date border-bot1px">
						<ul>
							<li>
								<div>日期</div>
								<div>
									<p><input type="radio" name="time" value="1" title="今日配餐" lay-filter="dataType" checked></p>
									<p><input type="radio" name="time" value="2" title="明日配餐" lay-filter="dataType"></p>
									<p><input type="radio" name="time" value="3" title="三日内配餐" lay-filter="dataType"></p>
									<p><input type="radio" name="time" value="5" title="本月配餐" lay-filter="dataType"></p>
									<p><input type="radio" name="time" value="6" title="下月配餐" lay-filter="dataType"></p>
									<p><input type="radio" name="time" value="4" title="更多日期" lay-filter="dataType"></p>
									<p class="layui-time-img">
												<span class="layui-input-inline" style="width: 160px;">
											      <input disabled type="text" name="price_min" autocomplete="off" class="layui-input test-item">
											    </span>
										<span>&nbsp;-&nbsp;</span>
										<span class="layui-input-inline" style="width: 160px;">
											      <input disabled type="text" name="price_max" autocomplete="off" class="layui-input test-item">
											    </span>
									</p>
								</div>
							</li>
							<li>
								<div>餐别</div>
								<div>
									<p><input type="radio" name="meal" value="" title="全部" checked></p>
									<p><input type="radio" name="meal" value="1" title="午餐"></p>
									<p><input type="radio" name="meal" value="2" title="晚餐"></p>
								</div>
							</li>
							<li>
								<div>配送状态</div>
								<div>
									<p><input type="radio" name="dispatching" value="" title="全部" checked></p>
									<p><input type="radio" name="dispatching" value="1" title="待配送"></p>
									<p><input type="radio" name="dispatching" value="2" title="已配送"></p>
								</div>
							</li>
						</ul>
						<div class="pt15px pb15px textRight">
							<span class="layui-btn" style="cursor: pointer" id="searchButton">查&nbsp;询</span>
							<span class="layui-btn layui-btn-primary" style="cursor: pointer" id="resetButton">重&nbsp;置</span>
						</div>
					</nav>
					<div class="more-table-con">
						<div class="number-per overflow textRight pt20px pb20px lh32px">
							<p class="bgGreen left border-radius3 white pl15px pr15px cursor" id="batchPrintButton"><img src="../static/img/icon_number_per.png" class="mr10px" style="vertical-align: -2px;">批量打印配送单</p>
							<p class="right">共有<span class="green" style="margin: 0 5px;">0</span>条记录</p>
						</div>
						<table class="layui-hide" id="test" lay-filter="test"></table>

					</div>
				</div>
			</div>
		</div>
	</div>

</div>
<script th:src="${#request.getContextPath()}+'/static/js/jquery-2.1.3.min.js'"></script>
<script>
	jQuery.browser={};
	(function(){
		jQuery.browser.msie=false;
		jQuery.browser.version=0;
		if(navigator.userAgent.match(/MSIE ([0-9]+)./)){
			jQuery.browser.msie=true;
			jQuery.browser.version=RegExp.$1;
		}
	})();
</script>
<script th:src="${#request.getContextPath()}+'/static/foodDelivery/js/jquery.jqprint-0.3.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/foodDelivery/js/socket.io.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/foodDelivery/hiprint/polyfill.min.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/foodDelivery/hiprint/hiprint.bundle.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/foodDelivery/hiprint/plugins/jquery.hiwprint.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/layui/layui.js'"></script>
<script>
	var orgId = "";
	var printData = [];
	var printIds = "";
	hiprint.init();

	//表格
	layui.use(['table',"laydate","form","layer"], function(){
		getOrganization();
		var table = layui.table;
		var laydate = layui.laydate;
		var form = layui.form;
		var layer = layui.layer;

		$("input.test-item").each(function (i,v) {
			laydate.render({
				elem: this,
				trigger: 'click'
			});
		})
		
		form.on("radio(dataType)",function (data) {
			if(data.value == "4"){
				$("input[name='price_min']").removeAttr("disabled").attr("readonly",true);
				$("input[name='price_max']").removeAttr("disabled").attr("readonly",true);
			}else{
				$("input[name='price_min']").val("").attr("disabled",true);
				$("input[name='price_max']").val("").attr("disabled",true);
			}
		})

		$("span#searchButton").on("click",function (e) {
			refresh();
		})

		$("span#resetButton").on("click",function (e) {
			$.each($("input[name='time']") ,function (index, even) {
				if(even.value == "1") {
					$(even).prop('checked',true);
					return false;
				}
			})
			$.each($("input[name='meal']") ,function (index, even) {
				if(! even.value) {
					$(even).prop('checked',true);
					return false;
				}
			})
			$.each($("input[name='dispatching']") ,function (index, even) {
				if(! even.value) {
					$(even).prop('checked',true);
					return false;
				}
			})
			$("input[name='price_min']").val("");
			$("input[name='price_max']").val("");
			form.render('radio');
			refresh();
		})

		//第一个实例
		table.render({
			elem: '#test'
			,url: '../ordering/getPlatformData'
			,page: true
			,id:"test"
			,method: "post"
			,request: {
				pageName: 'page',
				limitName: 'pageSize'
			}
			,where:{"dateType":$("input[name='time']:checked").val(),"type":$("input[name='meal']:checked").val(),"orderStatus":$("input[name='dispatching']:checked").val(),"startDate":$("input[name='price_min']").val(),"endDate":$("input[name='price_max']").val(),"orgId":orgId}
			,cols: [
					[
						{
							type: 'checkbox',
							fixed: 'left'
						}
						,
						{
							field: 'id',
							title: 'ID',
							hide: true
						}
						,
						{
							type: 'numbers',
							title: '序号',
							fixed: 'left',
							width:50
						}
						,
						{
							field: 'org_name',
							title: '村/社区'
						}
						,
						{
							field: 'food_date',
							title: '就餐日期',
						}
						,
						{
							field: 'type_str',
							title: '餐别',
							width:80
						}
						,
						{
							field: 'type',
							title: '午餐/晚餐',
							hide:true
						}
						,
						{
							field: '',
							title: '总数',
							width:80,
							templet: function(res){
								return (parseInt(res.count1)+parseInt(res.count2)+parseInt(res.count3));
							}
						}
						,
						{
							field: 'count1',
							title: '堂吃',
							width:80
						}
						,
						{
							field: 'count2',
							title: '自提',
							width:80
						}
						,
						{
							field: 'count3',
							title: '上门',
							width:80
						}
						,
						{
							field: 'date_time',
							title: '订餐时间',
							width:180
						}
						,
						{
							field: 'distribution_status',
							title: '状态',
							width:100
						}
						,
						{
							field: 'confirm_time',
							title: '确认时间',
							width:180
						}
						,
						{
							title: '操作',
							//style: 'color:#1C9399',
							//fixed: 'right',
							width:120,
							templet: function(res){
								//return "<div class='table-list-btn'>"+$("div#shiroPermission"+pageType).html()+"</div>"
								return "<div class='table-list-btn'><span class='goToDetails' lay-event='goToDetails'>详情</span></div>"
							}
						}
				]
			]
			,done:function(res, curr, count){
				let data = res.data;
				if(data && data.length > 0) {
					data.forEach(function (v,i) {
						if (printIds.indexOf(v.id + "|" + v.month + "|" + v.day + "|" + v.type) != -1) {
							$('div[lay-id="test"] table.layui-table tr[data-index=' + i + '] input[type="checkbox"]').prop('checked', true);
							$('div[lay-id="test"] table.layui-table tr[data-index=' + i + '] input[type="checkbox"]').next().addClass('layui-form-checked');
						}
					})
				}
			},
		});
		
		$("p#batchPrintButton").on("click",function (e) {
			showPrintHtml(null,"2");
		})

		table.on('tool(test)', function(obj){
			let data = obj.data;
			switch(obj.event){
				case 'goToDetails':
					let id = data.id;
					let month = data.month;
					let day = data.day;
					let type = data.type;
					showPrintHtml([{"id":id,"month":month,"day":day,"type":type}],"1");
					break;
			};
		});

		table.on('checkbox(test)', function(obj){
			if(obj.type == "all"){
				var tableAll = layui.table.cache.test;
				if(obj.checked){
					if(tableAll && tableAll.length > 0){
						$.each(tableAll,function (i,v) {
							if(! (printIds.indexOf(v.id + "|" + v.month + "|" + v.day + "|" + v.type) != -1)){
								printIds += v.id + "|" + v.month + "|" + v.day + "|" + v.type + ",";
								printData.push({"id":v.id,"month":v.month,"day":v.day,"type":v.type});
							}
						})
					}
				}else{
					if(tableAll && tableAll.length > 0){
						$.each(tableAll,function (i,v) {
							$.each(printData,function (j,w) {
								if(v.id == w.id && v.month == w.month && v.day == w.day && v.type == w.type){
									printIds = printIds.replace(v.id + "|" + v.month + "|" + v.day + "|" + v.type + ",","");
									printData.splice(j,1);
									return false;
								}
							})
						})
					}
				}
			}else if(obj.type == "one"){
				if(obj.checked){
					if(! (printIds.indexOf(obj.data.id + "|" + obj.data.month + "|" + obj.data.day + "|" + obj.data.type) != -1)){
						printIds += obj.data.id + "|" + obj.data.month + "|" + obj.data.day + "|" + obj.data.type + ",";
						printData.push({"id":obj.data.id,"month":obj.data.month,"day":obj.data.day,"type":obj.data.type});
					}
				}else{
					$.each(printData,function (j,w) {
						if(w.id == obj.data.id && w.month == obj.data.month && w.day == obj.data.day && w.type == obj.data.type){
							printIds = printIds.replace(w.id + "|" + w.month + "|" + w.day + "|" + w.type + ",","");
							printData.splice(j,1);
							return false;
						}
					})
				}
			}
		});

		function refresh(){
			let dateType = $("input[name='time']:checked").val();
			let type = $("input[name='meal']:checked").val();
			let orderStatus = $("input[name='dispatching']:checked").val();
			let startDate = $("input[name='price_min']").val();
			let endDate = $("input[name='price_max']").val();
			table.reload("test", {
				where:{"dateType":dateType,"type":type,"orderStatus":orderStatus,"startDate":startDate,"endDate":endDate,"orgId":orgId}
			});
		}

		function showPrintHtml(data,type) {
			if(type == "2"){
				data = {"data":JSON.stringify(printData)};
			}else{
				data = {"data":JSON.stringify(data)};
			}
			$.ajax({
				//url: "../ordering/testPrint",
				url: "../ordering/printOrder",
				type: "post",
				dataType: "json",
				//data: {"id":id,"month":month,"day":day,"type":type},
				data: data,
				success:function (result) {
					if(result.code == 200){
						let list = result.data;
						let html = '<div id="pop-con">\n' +
								'<div class="pop-up">\n' +
								'<div class="pop-title relative overflow mb15px pad20px"><span class="ml15px">配送单</span><p class="right pop-close"><img src="../static/img/close_icon.png" class="block pop-close"><p></div>\n' +
								'<article class="food-con lh32px">\n' +
								'<div class="add-box-scroll">';
						if(list && list.length > 0) {
							list.forEach(function (data,i) {
								html += '<nav class="mb20px border1px border-radius4 printNav pad20px">\n' +
										'<div class="fb textCenter">' + data.info.title + '</div>\n' +
										'<div class="textCenter">' + data.info.date + '</div>\n';
								if (data.fixedPoint) {
									html += '<div class="dashed-line relative textCenter">\n' +
											'<span class="bgWhite pl10px pr10px fb">堂吃/自提</span>\n' +
											'</div>\n' +
											'<ul>\n' +
											'<li>堂吃/自提 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x ' + data.fixedPoint.count + '</li>\n' +
											'<li>' + data.fixedPoint.address + '</li>\n' +
											'</ul>\n';
								}
								if (data.toDoor && data.toDoor.length > 0) {
									html += '<div class="dashed-line relative textCenter">\n' +
											'<span class="bgWhite pl10px pr10px fb">送餐上门</span>\n' +
											'</div>\n' +
											'<div>\n' +
											'<ul>\n' +
											'<li>堂吃/自提 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x ' + data.toDoor.length + '</li>\n' +

											'</ul>\n';
									data.toDoor.forEach(function (v, i) {
										html += '<ul><li>'+ getNotNull(v.user_name) +'&nbsp;&nbsp;&nbsp;' + getNotNull(v.mobile) + '</li><li>' + getNotNull(v.address) + '</li></ul>';
									})
								}
								html += '</div>\n' +
										'</nav>';

								/*html += '<nav style="height: auto" class="mb20px border1px border-radius4 pad20px printNav">\n' +
										'<div class="fb textCenter">' + data.info.title + '</div>\n' +
										'<div class="textCenter">' + data.info.date + '</div>\n';
								if (data.fixedPoint) {
									html += '<div class="dashed-line relative textCenter">\n' +
											'<span class="bgWhite pl10px pr10px fb">堂吃/自提</span>\n' +
											'</div>\n' +
											'<ul>\n' +
											'<li>堂吃/自提</li>\n' +
											'<li>\n' +
											'<p>x ' + data.fixedPoint.count + '</p>\n' +
											'<p>' + data.fixedPoint.address + '</p>\n' +
											'</li>\n' +
											'</ul>\n';
								}
								if (data.toDoor && data.toDoor.length > 0) {
									html += '<div class="dashed-line relative textCenter">\n' +
											'<span class="bgWhite pl10px pr10px fb">送餐上门</span>\n' +
											'</div>\n' +
											'<div>\n' +
											'<ul>\n' +
											'<li>送餐上门</li>\n' +
											'<li>\n' +
											'<p>x' + data.toDoor.length + '</p>\n' +
											'</li>\n' +
											'</ul>\n';
									data.toDoor.forEach(function (v, i) {
										html += '<ul><li>' + getNotNull(v.user_name) + '</li><li><p>' + getNotNull(v.mobile) + '</p><p>' + getNotNull(v.address) + '</p></li></ul>';
									})
								}
								html += '</div>\n' +
										'</nav>';*/
							})
						}
						html += '</div>\n' +
								'</article>\n' +
								'<div class="btn-con textCenter">\n' +
								'<span class="btn-one" style="cursor: pointer" id="printButton">打&nbsp;印</span>\n' +
								//'<span class="btn-two">重&nbsp;置</span>\n' +
								'</div>\n' +
								'</div>\n' +
								'</div>\n';
						layer.open({
							type: 1
							,title: false //不显示标题栏
							,closeBtn: false
							,area: ['390px','']
							,shade: 0.6
							,id: 'payCons' //设定一个id，防止重复弹出
							,btnAlign: 'c'
							,moveType: 1 //拖拽模式，0或者1
							,shadeClose: false //设置允许点遮罩关闭
							,content: html,
							success:function(){
								//关闭弹层
								$(".pop-close").on("click",function(){
									if(type == "2") {
										printData = [];
										printIds = "";
									}
									layer.closeAll();
								});

								$("span#printButton").on("click",function (e) {
									layer.closeAll();
									printOrder(list);
									/*$("div#pop-con nav.printNav").each(function (i,v) {
										$(this).jqprint();
									})*/
								})

								/*$("span#printButton").on("click",function (e) {
									layer.closeAll();
									$.ajax({
										url:"../ordering/printOrder",
										type:"post",
										dataType:"json",
										data:{"data":JSON.stringify(printData)},
										success:function (result) {
											if(type == "2") {
												printData = [];
												printIds = "";
												$('div[lay-id="test"] table.layui-table tr input[type="checkbox"]').prop('checked', false);
												$('div[lay-id="test"] table.layui-table tr input[type="checkbox"]').next().removeClass('layui-form-checked');
											}
											layer.msg(result.msg)
										}
									})
								})*/
							},

						});
					}
				}
			})
		}
	});


	function printOrder(data) {
		if(data && data.length > 0){
			let templates = [];
			data.forEach(function (v,i) {
				let templateConfig = getTemplateConfig(v);
				if(templateConfig){
					let template = new hiprint.PrintTemplate({
						template: templateConfig.json
					})
					templates.push({template:template,data:templateConfig.data,options:{}});
				}
			});
			hiprint.print2({ templates: templates }, function (e) { console.log('打印成功！') });
		}
	}


	function getTemplateConfig(data) {
		let result = {};
		if(data){
			let info = data.info;
			let fixedPoint = data.fixedPoint;
			let toDoor = data.toDoor;
			let templateData = {"title":info.title,"date":info.date,"tczt_title":"堂吃/自提","tczt":"堂吃/自提","tczt_count":"x "+fixedPoint.count,"tczt_address":fixedPoint.address};
			let printElements = [
				{
					"options":{
						"left":0,
						"top":20,
						"height":22,
						"width":226.5,
						"field":"title",
						"fontSize":18,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"textAlign":"center",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				},
				{
					"options":{
						"left":0,
						"top":50,
						"height":20,
						"width":226.5,
						"field":"date",
						"fontSize":18,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"textAlign":"center",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				},
				{
					"options":{
						"left":0,
						"top":80,
						"height":20,
						"width":226.5,
						"field":"tczt_title",
						"fontSize":15,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"textAlign":"center",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				},
				{
					"options":{
						"left":13.5,
						"top":110,
						"height":20,
						"width":60,
						"fontSize":12,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"field":"tczt",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				},
				{
					"options":{
						"left":84,
						"top":110,
						"height":20,
						"width":142.5,
						"fontSize":12,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"field":"tczt_count",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				},
				{
					"options":{
						"left":13.5,
						"top":140,
						"height":25,
						"width":213,
						"fontSize":12,
						"fontFamily":"Microsoft YaHei",
						"fontWeight":"500",
						"field":"tczt_address",
						"textContentVerticalAlign":"middle"
					},
					"printElementType":{
						"type":"text"
					}
				}
			];
			if(toDoor && toDoor.length > 0){
				templateData.scsm_title = "送餐上门";
				templateData.scsm = "送餐上门";
				templateData.scsm_count = "x " + toDoor.length;
				printElements.push({"options":{"left":0,"top":170,"height":22,"width":226.5,"fontSize":15,"fontFamily":"Microsoft YaHei","fontWeight":"500","field":"scsm_title","textAlign":"center","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
				printElements.push({"options":{"left":13.5,"top":200,"height":20,"width":60,"field":"scsm","fontSize":12,"fontFamily":"Microsoft YaHei","fontWeight":"500","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
				printElements.push({"options":{"left":84,"top":200,"height":20,"width":142.5,"field":"scsm_count","fontSize":12,"fontFamily":"Microsoft YaHei","fontWeight":"500","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
				let top = 230;
				for(let i = 1 ;i <= toDoor.length ; i ++){
					templateData["user_phone"+i] = getNotNull(toDoor[i-1].mobile);
					templateData["user_name"+i] = getNotNull(toDoor[i-1].user_name);
					templateData["user_address"+i] = getNotNull(toDoor[i-1].address);
					printElements.push({"options":{"left":84,"top":top,"height":20,"width":142.5,"field":"user_phone"+i,"fontSize":12,"fontFamily":"Microsoft YaHei","fontWeight":"500","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
					printElements.push({"options":{"left":13.5,"top":top,"height":20,"width":60,"field":"user_name"+i,"fontSize":12,"fontFamily":"Microsoft YaHei","fontWeight":"500","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
					top += 30;
					printElements.push({"options":{"left":13.5,"top":top,"height":25,"width":213,"field":"user_address"+i,"fontSize":12,"fontFamily":"Microsoft YaHei","fontWeight":"500","textContentVerticalAlign":"middle"},"printElementType":{"type":"text"}});
					top += 30;
				}
			}
			let templateJson = {"panels":[
					{
						"index":0,
						"height":3000,
						"width":80,
						"paperNumberDisabled":true,
						"orient":1,
						"printElements": printElements
					}
				]};
			result.json = templateJson;
			result.data = templateData;
		}
		return result;
	}


	//获取组织架构
	function getOrganization() {
		var formDate = {"type":3,"userFlag":"yes"};
		$.ajax({
			url: "../org/getAllAuthTypeOrg",
			type: "post",
			dataType: "json",
			data: formDate,
			async: false,
			success: function (res) {
				var organizationList = res.children;
				var str = "";

				var whichFloor = 1;
				var uLHasNum = switchCss(whichFloor);


				str += "<ul class=" + uLHasNum + ">";
				$.each(organizationList, function (key, rog1) {

					str += '<li>';
					str += '<div class="tree-children" >';
					str += '<div>';

					str += "<p class='tree-arrow cursor'><img src='../static/img/icon_select_up.png'></p>"
					str += '<p style="width: 50px" data-orgid="' + rog1.orgId + '">' + rog1.orgName + '</p>';
					str += '</div>';
					str += '</div>';

					if (rog1.isChildExist == '1' && !isNull(rog1.children) && rog1.children.length > 0) {
						str += splicingMainStr(rog1, whichFloor);
					}
					str += '</li>';
				});
				str += '</ul>';

				$("#organizationDiv").html(str);
				//初始化
				$('.tree-one div.tree-children div p.tree-arrow').click(function() {
					$(this).parent().parents('div.tree-children').nextAll('ul').slideToggle(300);
					if ($(this).find('img').attr('src').indexOf('img/icon_select_up.png') != -1) {
						$(this).find('img').attr('src', '../static/img/icon_select_down.png');
						$(this).parent().siblings().find('img').attr('src', '../static/img/icon_select_up.png');
					} else {
						$(this).find('img').attr('src', '../static/img/icon_select_up.png')
					}
				})
				$("div.tree-children > div > p:not(.tree-arrow)").on("click",function(e){
					$("div.tree-children > div > p:not(.tree-arrow)").parent().parent().removeClass("current");
					$(this).parent().parent().addClass("current");
					orgId = $(this).data("orgid");
					if(orgId == "10" || orgId == 10){orgId = "";}
					$("span#searchButton").trigger("click");
				})

				$('.width-two-tree .tree-con-box').hide();
				$('.add-mess-form-tree').click(function(){
					$(this).next().slideToggle();
				})
			}
		});

	}

	//拼接
	function splicingMainStr(rog1, whichFloor) {
		var str = "";
		if (rog1.isChildExist == '1' && !isNull(rog1.children) && rog1.children.length > 0) {
			whichFloor = whichFloor + 1;
			var uLHasNum = switchCss(whichFloor);

			str += '<ul class=" ' + uLHasNum + '">';

			$.each(rog1.children, function (key, rog2) {
				if (rog2.isChildExist == '1' && !isNull(rog2.children) && rog2.children.length > 0) {
					str += '<li>';
					str += '<div class="tree-children">';
					str += '<div>';
					str += "<p class='tree-arrow cursor'><img src='../static/img/icon_select_up.png'></p>"
					str += '<p data-orgid="' + rog2.orgId + '">' + rog2.orgName + '</p>';
					str += '</div>';
					str += '</div>';
					str += splicingMainStr(rog2, whichFloor);
					str += '</li>';
				} else {
					if (!isNull(rog2)) {
						str += splicingStr(rog2);
					}
				}
			})
			str += '</ul>';

		} else {
			if (!isNull(rog1)) {
				str += splicingStr(rog1);
			}
		}
		return str;
	}

	//层级转换标签
	function switchCss(whichFloor) {

		var uLHasNum = "";

		switch (whichFloor) {
			case 1:
				uLHasNum = "tree-one";
				break;
			case 2:
				uLHasNum = "tree-two";
				break;
			case 3:
				uLHasNum = "tree-three";
				break;
			case 4:
				uLHasNum = "tree-four";
				break;
			case 5:
				uLHasNum = "tree-five";
		}
		return uLHasNum;
	}

	//拼接
	function splicingStr(org) {
		var str = "";
		//是否是行政机构 0-否，1-是    type 是否实际勾选 1-是 0-否
		str += '<li>';
		str += '<div class="tree-children">';
		str += '<div>';
		str += '<p data-orgid="' + org.orgId + '" >' + org.orgName + '</p>';
		str += '</div>';
		str += '</div>';
		str += '</li>';
		return str;
	}

	function isNull(value) {
		return value == null || value === "" || value === undefined;
	}

	function getNotNull(value) {
		if(value && $.trim(value) && value != "null"){
			return value;
		}else{
			return "";
		}
	}
</script>
</body>

</html>